package com.richapp.launcher.gpio;

import android.util.Log;

import com.jack.netty.log.LoggerManager;
import com.softwinner.Gpio;

public class GPIOServer {

	
	private static GPIOServer instance = null;
	private int base = 200;
	private final String TAG = GPIOServer.class.getName();

    //开关位
    private int VALUE_OPEN = 1;
    private int VALUE_CLOSE = 0;

    //蜂鸣器
    private char GROUP_FENG_MING_QI = 'A';//H
    private int ID_FENG_MING_QI = 15;//28

    //wifi
    private char GROUP_WIFI = 'E';
    private int ID_WIFI = 00;

    //lan口
    private char GROUP_LAN = 'E';
    private int ID_LAN = 01;

    //yellow led
    private char GROUP_YELLOW_LED = 'E';
    private int ID_YELLOW_LED = 02;


	private GPIOServer() {
		
	}
	
	public static GPIOServer getInstance(){
		if(instance == null){
			instance = new GPIOServer();
		}
		return instance;
	}

    /**
     * 所有灯闪光一变
     */
    public void lightFlash(){

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    int oldLanValue = Gpio.readGpio(GROUP_LAN,ID_LAN);
                    int oldWifiValue = Gpio.readGpio(GROUP_WIFI,ID_WIFI);
                    int oldYellowValue = Gpio.readGpio(GROUP_YELLOW_LED,ID_YELLOW_LED);

                    if(oldLanValue == 1){
                        Gpio.writeGpio(GROUP_LAN,ID_LAN,VALUE_CLOSE);
                    }else{
                        Gpio.writeGpio(GROUP_LAN,ID_LAN,VALUE_OPEN);
                    }

                    if(oldWifiValue == 1){
                        Gpio.writeGpio(GROUP_WIFI,ID_WIFI,VALUE_CLOSE);
                    }else{
                        Gpio.writeGpio(GROUP_WIFI,ID_WIFI,VALUE_OPEN);
                    }

                    if(oldYellowValue == 1){
                        Gpio.writeGpio(GROUP_YELLOW_LED,ID_YELLOW_LED,VALUE_CLOSE);
                    }else{
                        Gpio.writeGpio(GROUP_YELLOW_LED,ID_YELLOW_LED,VALUE_OPEN);
                    }

                    Thread.sleep(base);
                    Gpio.writeGpio(GROUP_LAN,ID_LAN,oldLanValue);
                    Gpio.writeGpio(GROUP_WIFI,ID_WIFI,oldWifiValue);
                    Gpio.writeGpio(GROUP_YELLOW_LED,ID_YELLOW_LED,oldYellowValue);

                    Thread.sleep(base);
                    Gpio.writeGpio(GROUP_YELLOW_LED,ID_YELLOW_LED,0);
                    Gpio.writeGpio(GROUP_WIFI,ID_WIFI,0);
                    Gpio.writeGpio(GROUP_LAN,ID_LAN,0);
                }catch (Exception e){
                    Gpio.writeGpio(GROUP_YELLOW_LED,ID_YELLOW_LED,0);
                    Gpio.writeGpio(GROUP_WIFI,ID_WIFI,0);
                    Gpio.writeGpio(GROUP_LAN,ID_LAN,0);
                    Log.e(TAG, e.getMessage());

                }
            }
        }).start();
    }

    //wifi light open
    public void openWifiLight(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Gpio.writeGpio(GROUP_WIFI,ID_WIFI,VALUE_CLOSE);
                }catch (Exception e){
                    LoggerManager.getInstance().getiLogger().error(getClass(), e.getMessage());
                }
            }
        }).start();
    }

    //wifi light close
    public void closeWifiLight(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Gpio.writeGpio(GROUP_WIFI,ID_WIFI,VALUE_OPEN);
                }catch (Exception e){
                    LoggerManager.getInstance().getiLogger().error(getClass(), e.getMessage());
                }
            }
        }).start();
    }

    //lan ok
    public void openLanLight(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Gpio.writeGpio(GROUP_LAN,ID_LAN,VALUE_CLOSE);
                }catch (Exception e){
                    LoggerManager.getInstance().getiLogger().error(getClass(),e.getMessage());
                }
            }
        }).start();
    }

    //lan no
    public void closeLanLight(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Gpio.writeGpio(GROUP_LAN,ID_LAN,VALUE_OPEN);
                }catch (Exception e){
                    LoggerManager.getInstance().getiLogger().error(getClass(),e.getMessage());
                }
            }
        }).start();
    }

    //YELLOW LED OPEN
    public void openYellowLedLight(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Gpio.writeGpio(GROUP_YELLOW_LED,ID_YELLOW_LED,VALUE_CLOSE);
                }catch (Exception e){
                    LoggerManager.getInstance().getiLogger().error(getClass(),e.getMessage());
                }
            }
        }).start();
    }

    //YELLOW LED CLOSE
    public void closeYellowLedLight(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Gpio.writeGpio(GROUP_YELLOW_LED,ID_YELLOW_LED,VALUE_OPEN);
                }catch (Exception e){
                    LoggerManager.getInstance().getiLogger().error(getClass(),e.getMessage());
                }
            }
        }).start();
    }
	
	//盲添开始beep
	public void mindAddStartBeep(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                    Thread.sleep(2*base);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        }).start();
	}

	//盲添结束beep
	public void mindAddStopBeep(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                    Thread.sleep(base);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                    Thread.sleep(base);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                    Thread.sleep(base);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        }).start();
	}
	
	//报警BEEP
	public void alarmBeep(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for(int i = 0 ; i < 3 ; i++){
                        Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                        Thread.sleep(base/2);
                        Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                        Thread.sleep(base/2);
                        Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                        Thread.sleep(base/2);
                        Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                        Thread.sleep(base/2);
                        Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                        Thread.sleep(base/2);
                        Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);

                        Thread.sleep(base);

                        Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                        Thread.sleep(base/2);
                        Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                        Thread.sleep(base/2);
                        Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                        Thread.sleep(base/2);
                        Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                        Thread.sleep(base/2);
                        Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                        Thread.sleep(base/2);
                        Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);

                        Thread.sleep(3*base);
                    }
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        }).start();
	}

    public void infraredStudyStart(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                    Thread.sleep(1000);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        }).start();
    }

    public void infraredStudySuccessed(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                    Thread.sleep(500);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                    Thread.sleep(base/2);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                    Thread.sleep(500);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        }).start();
    }

    public void infraredStudyFailed(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                    Thread.sleep(330);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                    Thread.sleep(base/2);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                    Thread.sleep(330);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                    Thread.sleep(base/2);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_OPEN);
                    Thread.sleep(330);
                    Gpio.writeGpio(GROUP_FENG_MING_QI, ID_FENG_MING_QI, VALUE_CLOSE);
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        }).start();
    }

    public int getBase() {
        return base;
    }

    public void setBase(int base) {
        this.base = base;
    }

    public int getVALUE_OPEN() {
        return VALUE_OPEN;
    }

    public void setVALUE_OPEN(int VALUE_OPEN) {
        this.VALUE_OPEN = VALUE_OPEN;
    }

    public int getVALUE_CLOSE() {
        return VALUE_CLOSE;
    }

    public void setVALUE_CLOSE(int VALUE_CLOSE) {
        this.VALUE_CLOSE = VALUE_CLOSE;
    }

    public char getGROUP_FENG_MING_QI() {
        return GROUP_FENG_MING_QI;
    }

    public void setGROUP_FENG_MING_QI(char GROUP_FENG_MING_QI) {
        this.GROUP_FENG_MING_QI = GROUP_FENG_MING_QI;
    }

    public int getID_FENG_MING_QI() {
        return ID_FENG_MING_QI;
    }

    public void setID_FENG_MING_QI(int ID_FENG_MING_QI) {
        this.ID_FENG_MING_QI = ID_FENG_MING_QI;
    }

    public char getGROUP_WIFI() {
        return GROUP_WIFI;
    }

    public void setGROUP_WIFI(char GROUP_WIFI) {
        this.GROUP_WIFI = GROUP_WIFI;
    }

    public int getID_WIFI() {
        return ID_WIFI;
    }

    public void setID_WIFI(int ID_WIFI) {
        this.ID_WIFI = ID_WIFI;
    }

    public char getGROUP_LAN() {
        return GROUP_LAN;
    }

    public void setGROUP_LAN(char GROUP_LAN) {
        this.GROUP_LAN = GROUP_LAN;
    }

    public int getID_LAN() {
        return ID_LAN;
    }

    public void setID_LAN(int ID_LAN) {
        this.ID_LAN = ID_LAN;
    }

    public char getGROUP_YELLOW_LED() {
        return GROUP_YELLOW_LED;
    }

    public void setGROUP_YELLOW_LED(char GROUP_YELLOW_LED) {
        this.GROUP_YELLOW_LED = GROUP_YELLOW_LED;
    }

    public int getID_YELLOW_LED() {
        return ID_YELLOW_LED;
    }

    public void setID_YELLOW_LED(int ID_YELLOW_LED) {
        this.ID_YELLOW_LED = ID_YELLOW_LED;
    }
}
